package cn.merchant.service.impl;

import cn.itcast.mapper.merchantMapper;
import cn.itcast.mapper.rechargeMapper;
import cn.itcast.pojo.*;
import cn.merchant.service.payService;
import cn.itcast.util.DoubleUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@SuppressWarnings("ALL")
@Slf4j
@Service
public class MerchantPayServiceImpl implements payService {

    @Resource
    cn.itcast.mapper.accountMapper accountMapper;

    @Resource
    merchantMapper merchantMapper;

    @Resource
    rechargeMapper rechargeMapper;

    @Resource
    cn.itcast.mapper.userMapper userMapper;

    @Resource
    cn.itcast.mapper.withdrawMapper withdrawMapper;



    @Override
    public List<account> findHoresmanAccount(String openId) {
        accountExample aEx = new accountExample();
        accountExample.Criteria criteria = aEx.createCriteria();
        criteria.andOpenIdEqualTo(openId);
        criteria.andAccountTypeidEqualTo(1);
        List<account> accounts = accountMapper.selectByExample(aEx);
        return accounts;
    }

    @Override
    public merchant queryByOpenId(String openId) {
        merchantExample merchantExample = new merchantExample();
        merchantExample.Criteria criteria = merchantExample.createCriteria();
        criteria.andOpenIdEqualTo(openId);
        List<merchant> merchants = merchantMapper.selectByExample(merchantExample);
        if(merchants.isEmpty()){
            return null;
        }else {
            return merchants.get(0);
        }
    }

    @Override
    public account queryAccountByOpenId(String openid) {
        accountExample accountExample = new accountExample();
        accountExample.Criteria criteria = accountExample.createCriteria();
        criteria.andOpenIdEqualTo(openid);
        criteria.andAccountNumberEqualTo("0301");
        List<account> accounts = accountMapper.selectByExample(accountExample);
        if(accounts.isEmpty()){
            return null;
        }else {
            return accounts.get(0);
        }

    }

    @Override
    public List<recharge> findrechargeRecord(String openId) {
        rechargeExample rechargeExample = new rechargeExample();
        rechargeExample.Criteria criteria = rechargeExample.createCriteria();
        criteria.andOpenIdEqualTo(openId);
        return rechargeMapper.selectByExample(rechargeExample);
    }

    @Override
    public user queryByUserOpenId(String openid) {
        userExample userExample = new userExample();
        userExample.Criteria criteria = userExample.createCriteria();
        criteria.andOpenIdEqualTo(openid);
        List<user> users = userMapper.selectByExample(userExample);
        if(users.isEmpty()){
            return null;
        }else {
            return users.get(0);
        }
    }

    @Override
    public account queryAccountByOpenIdByUser(String openid) {
        accountExample accountExample = new accountExample();
        accountExample.Criteria criteria = accountExample.createCriteria();
        criteria.andOpenIdEqualTo(openid);
        criteria.andAccountNumberEqualTo("0101");
        List<account> accounts = accountMapper.selectByExample(accountExample);
        if(accounts.isEmpty()){
            return null;
        }else {
            return accounts.get(0);
        }

    }

    @Override
    public boolean updateHoresmanAccount(account account,double amount,withdraw withdraw) {
        account.setAccountMoneyBalance(DoubleUtils.sub(account.getAccountMoneyBalance(),amount));
        log.debug("提现后账户金额");
        log.debug(account.getAccountMoneyBalance().toString());
        int i = accountMapper.updateByPrimaryKeySelective(account);
        if(i==1){
            //修改提现记录状态
            withdraw.setState("已到账");
            withdraw.setBillTime(new Date());
            withdrawMapper.updateByPrimaryKeySelective(withdraw);
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean updateUserAccount(account account,double amount,withdraw withdraw) {
        account.setAccountMoneyBalance(DoubleUtils.sub(account.getAccountMoneyBalance(),amount));
        log.debug("提现后账户金额");
        log.debug(account.getAccountMoneyBalance().toString());
        int i = accountMapper.updateByPrimaryKeySelective(account);
        if(i==1){
            //修改提现记录状态
            withdraw.setState("已到账");
            withdraw.setBillTime(new Date());
            withdrawMapper.updateByPrimaryKeySelective(withdraw);

            return true;
        }else {
            return false;
        }
    }

    @Override
    public withdraw createWithDraw(withdraw withdraw) {
        int i = withdrawMapper.insertSelective(withdraw);
        if(i==1){
            return withdraw;
        }else {
            return null;
        }
    }

    @Override
    public List<withdraw> findWithdrawRecord(String openId) {
        withdrawExample wEx = new withdrawExample();
        withdrawExample.Criteria criteria = wEx.createCriteria();
        criteria.andOpenIdEqualTo(openId);
        return withdrawMapper.selectByExample(wEx);

    }

    @Override
    public withdraw createWithDrawByHoresman(withdraw withdraw) {
        return null;
    }

}
